package org.tls.ylt.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.tls.ylt.model.acl.User;
import org.tls.ylt.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.tls.ylt.common.exception.MyException;
import org.tls.ylt.common.result.Result;
import org.tls.ylt.common.result.ResultCodeEnum;
import org.tls.ylt.common.utils.MD5;

import org.springframework.beans.factory.annotation.Autowired;
import org.tls.ylt.model.hosp.HospitalSet;
import org.tls.ylt.hosp.service.HospitalSetService;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Api("医院设置")
@Controller
@ResponseBody
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @GetMapping("getHospitalSet/{id}")
    Result getHospSet(@PathVariable Long id) {
        return Result.ok(hospitalSetService.getById(id));
    }

    @ApiOperation("获取所有")
    @GetMapping("findAll")
    public Result<List<HospitalSet>> getAll() {
        List<HospitalSet> list = hospitalSetService.list();
        System.out.println(list);
        return Result.ok(list);
    }

    @GetMapping("getCharacter/{id}")
    Result getCharacterId(@PathVariable Long id) {
        return Result.ok(hospitalSetService.getById(id));
    }

    @GetMapping("getCharacter")
    Result getCharacter(@RequestParam Long id, ServletRequest request) throws IOException {
        return Result.ok(hospitalSetService.getById(id));
    }

    @GetMapping("getPic")
    void getPic(ServletResponse response) throws IOException {
        byte[] b = new byte[1024];
        BufferedInputStream is = new BufferedInputStream(new FileInputStream("T:\\素材\\素材-图片\\axsaaa.jpg"));
        OutputStream os = response.getOutputStream();
        while (is.read(b) != -1) {
            os.write(b);
        }
        os.flush();
        os.close();
    }

    @PostMapping("postCharacter")
    Result postCharacter(@RequestBody(required = false) User user, ServletRequest request) throws IOException {
        /*BufferedReader bf = new BufferedReader(new InputStreamReader(request.getInputStream()));
        StringBuilder sb= new StringBuilder();
        String s=null;
        if((s = bf.readLine()) !=null){
            sb.append(s);
        }
        System.out.println(sb.toString());*/

        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println(user);
        return Result.ok();
    }

    @PostMapping("postPic")
    void postPic(@RequestBody Long id, ServletResponse response) throws IOException {
        byte[] b = new byte[1024];
        BufferedInputStream is = new BufferedInputStream(new FileInputStream("T:\\素材\\素材-图片\\axsaaa.jpg"));
        OutputStream os = response.getOutputStream();
        System.out.println(id);
        while (is.read(b) != -1) {
            os.write(b);
        }
        os.flush();
        os.close();
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("{id}")
    Result deleteById(@PathVariable("id") Long id) {
        boolean b = hospitalSetService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @PostMapping("findPageHospSet/{current}/{limit}")
    Result findPageHospSet(@PathVariable long current, @PathVariable long limit,
                           @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> myPage = new Page<>();
        myPage.setCurrent(current);
        myPage.setSize(limit);
        System.out.println(myPage.getCurrent() + "," + myPage.getSize());
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();
        System.out.println(hospitalSetQueryVo);
        if (!StringUtils.isEmpty(hoscode)) {
            queryWrapper.like("hoscode", hoscode);
        }
        if (!StringUtils.isEmpty(hosname)) {
            queryWrapper.eq("hosname", hosname);
        }
        System.out.println("开始查询");
        Page<HospitalSet> page = hospitalSetService.page(myPage, queryWrapper);
        return Result.ok(page);
    }

    @PostMapping("saveHospitalSet")
    Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(1);
        Random random = new Random();
        System.out.println("--------------");
        System.out.println(random + "," + random.nextInt(1000));
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    @PostMapping("updateHospitalSet")
    Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @DeleteMapping("batchRemove")
    Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        boolean b = hospitalSetService.removeByIds(idList);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @PutMapping("lockHospitalSet/{id}/{status}")
    Result lockHospitalSet(@PathVariable Long id,
                           @PathVariable Integer status) {
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        //这里是动态sql，对于null值不会覆盖，所以不需要先根据id查出数据，再set
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @PutMapping("sendKey/{id}")
    Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //短信操作
        throw new MyException(ResultCodeEnum.FAIL);
        //return Result.ok();
    }
}
